﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Kobets.Automation.Chimera.Common
{
    /// <summary>
    /// ObservableCollection that is Unique
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ObservableUniqueCollection<T> : ObservableCollection<T>
    {
        protected override void InsertItem(int index, T item)
        {
            var exists = false;

            foreach (var myItem in Items.Where(myItem => myItem.Equals(item)))
                exists = true;

            if (!exists)
                base.InsertItem(index, item);
        }

        public void AddRange(IEnumerable<T> range)
        {
            if (range == null) throw new ArgumentNullException("range");

            foreach (var item in range)
                Add(item);
        }
    }
}
